<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 环境变量 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 环境变量</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<h2 id="显示环境变量"><a href="#显示环境变量" class="headerlink" title="显示环境变量"></a>显示环境变量</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> $[变量名]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>示例：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> <span class="variable">$HOME</span></div><div class="line">/root</div></pre></td></tr></table></figure>
<h2 id="设置一个新的环境变量"><a href="#设置一个新的环境变量" class="headerlink" title="设置一个新的环境变量"></a>设置一个新的环境变量</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">export</span> HELLO=<span class="string">"Hello!"</span></div><div class="line">$ <span class="built_in">echo</span> <span class="variable">$HELLO</span></div><div class="line">Hello!</div></pre></td></tr></table></figure>
<h3 id="查看全局环境变量命令：env"><a href="#查看全局环境变量命令：env" class="headerlink" title="查看全局环境变量命令：env"></a>查看全局环境变量命令：env</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>使用 <code>env</code> 命令显示所有的环境变量</strong>   </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$ env</div><div class="line">HOSTNAME=redbooks.safe.org</div><div class="line">PVM_RSH=/usr/bin/rsh</div><div class="line">Shell=/bin/bash</div><div class="line">TERM=xterm</div><div class="line">HISTSIZE=1000</div></pre></td></tr></table></figure>
<h2 id="查看所有环境变量命令：set"><a href="#查看所有环境变量命令：set" class="headerlink" title="查看所有环境变量命令：set"></a>查看所有环境变量命令：set</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>使用 <code>set</code> 命令显示所有本地定义的 shell 变量</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">set</span></div><div class="line">BASH=/bin/bash</div><div class="line">BASH_VERSINFO=([0]=<span class="string">"2"</span>[1]=<span class="string">"05b"</span>[2]=<span class="string">"0"</span>[3]=<span class="string">"1"</span>[4]=<span class="string">"release"</span>[5]=<span class="string">"i386-redhat-linux-gnu"</span>)</div><div class="line">BASH_VERSION=<span class="string">'2.05b.0(1)-release'</span></div><div class="line">COLORS=/etc/DIR_COLORS.xterm</div><div class="line">COLUMNS=80</div><div class="line">DIRSTACK=()</div><div class="line">DISPLAY=:0.0</div></pre></td></tr></table></figure>
<h2 id="删除一个变量：unset"><a href="#删除一个变量：unset" class="headerlink" title="删除一个变量：unset"></a>删除一个变量：unset</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong>  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">unset</span> [变量名]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只针对当前会话</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;set可以设置某个环境变量的值。清除环境变量的值用unset命令。如果未指定值，则该变量值将被设为NULL。示例如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">export</span> TEST=<span class="string">"Test..."</span> <span class="comment">#增加一个环境变量TEST</span></div><div class="line">$ env|grep TEST <span class="comment">#此命令有输入，证明环境变量TEST已经存在了</span></div><div class="line">TEST=Test...</div><div class="line">$ <span class="built_in">unset</span> <span class="variable">$TEST</span> <span class="comment">#删除环境变量TEST</span></div><div class="line">$ env|grep TEST <span class="comment">#此命令没有输出，证明环境变量TEST已经存在了</span></div></pre></td></tr></table></figure>
<h2 id="使用-readonly-命令设置只读变量"><a href="#使用-readonly-命令设置只读变量" class="headerlink" title="使用 readonly 命令设置只读变量"></a>使用 readonly 命令设置只读变量</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果使用了 readonly 命令，变量就不可以被修改或清除了<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;示例如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">export</span> TEST=<span class="string">"Test..."</span> <span class="comment">#增加一个环境变量TEST</span></div><div class="line">$ <span class="built_in">readonly</span> TEST <span class="comment">#将环境变量TEST设为只读</span></div><div class="line">$ <span class="built_in">unset</span> TEST <span class="comment">#会发现此变量不能被删除</span></div><div class="line">-bash: <span class="built_in">unset</span>: TEST: cannot <span class="built_in">unset</span>: <span class="built_in">readonly</span> variable</div><div class="line">$ TEST=<span class="string">"New"</span> <span class="comment">#会发现此也变量不能被修改</span></div><div class="line">-bash: TEST: <span class="built_in">readonly</span> variable</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境变量的设置位于 <code>/etc/profile</code> 文件，如果需要增加新的环境变量可以添加下属行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/PATH1:/PATH2:/PATHN</div></pre></td></tr></table></figure>
<h2 id="Linux-的变量种类"><a href="#Linux-的变量种类" class="headerlink" title="Linux 的变量种类"></a>Linux 的变量种类</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;按变量的生存周期来划分，Linux 变量可范围两类：<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1、永久的：需要修改配置文件，变量永久生效。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2、临时的：使用 export 命令声明即可，变量在关闭 shell 时失效。</p>
<h2 id="环境变量的配置文件"><a href="#环境变量的配置文件" class="headerlink" title="环境变量的配置文件"></a>环境变量的配置文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;全局： <code>/etc/profile</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;局部： <code>~/.bash_profile</code></p>
<h2 id="设置变量的方法"><a href="#设置变量的方法" class="headerlink" title="设置变量的方法"></a>设置变量的方法</h2><h3 id="1-在-etc-profile-文件中添加变量｛对所有用户生效（永久的）｝"><a href="#1-在-etc-profile-文件中添加变量｛对所有用户生效（永久的）｝" class="headerlink" title="1.在 /etc/profile 文件中添加变量｛对所有用户生效（永久的）｝"></a>1.在 /etc/profile 文件中添加变量｛对所有用户生效（永久的）｝</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用 <code>vim</code> 在文件 <code>/etc/profile</code> 文件中增加变量，该变量将会对 Linux下 所有用户有效，并且是“永久的”。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：编辑 /etc/profile 文件，添加CLASSPATH变量   </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># vim /etc/profile</span></div><div class="line"><span class="built_in">export</span> CLASSPATH=./JAVA_HOME/lib;<span class="variable">$JAVA_HOME</span>/jre/lib</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：修改文件后要想马上生效还要运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># source /etc/profile</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不然只能在下次重进此用户时生效</p>
<h3 id="2-在用户目录下个-bash-profile-文件中增加变量｛对单一用户生效（永久的）｝"><a href="#2-在用户目录下个-bash-profile-文件中增加变量｛对单一用户生效（永久的）｝" class="headerlink" title="2.在用户目录下个 .bash_profile 文件中增加变量｛对单一用户生效（永久的）｝"></a>2.在用户目录下个 <code>.bash_profile</code> 文件中增加变量｛对单一用户生效（永久的）｝</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用 <code>vim</code>在用户目录下的 <code>.bash_profile</code> 文件中增加变量，改变量仅会对当前用户有效，并且是“永久的”。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：编辑guok用户目录(/home/guok)下的.bash_profile   </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ vim /home/guok/.bash.profile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加如下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> CLASSPATH=./JAVA_HOME/lib;<span class="variable">$JAVA_HOME</span>/jre/lib</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：修改文件后要想马上生效还要运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">source</span> /home/guok/.bash_profile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不然只能在下次重进此用户时生效</p>
<h3 id="3-直接运行-export-命令定义变量｛只对当前-shell-（BASH）有效（临时的）｝"><a href="#3-直接运行-export-命令定义变量｛只对当前-shell-（BASH）有效（临时的）｝" class="headerlink" title="3.直接运行 export 命令定义变量｛只对当前 shell （BASH）有效（临时的）｝"></a>3.直接运行 export 命令定义变量｛只对当前 shell （BASH）有效（临时的）｝</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_HOME=/usr/<span class="built_in">local</span>/java   <span class="comment">#添加新变量名</span></div><div class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/usr/<span class="built_in">local</span>/php/bin <span class="comment">#修改已有变量名</span></div></pre></td></tr></table></figure>
<h2 id="常用的环境变量"><a href="#常用的环境变量" class="headerlink" title="常用的环境变量"></a>常用的环境变量</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;PATH：决定了shell将到哪些目录中寻找命令或程序，PATH的值是一系列以冒号分割的目录<br>注意：最好不要把 “./“ 放到 PATH 中，这样会引起安全问题</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HOME：当前用户主目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HISTSIZE：历史记录数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;LOGNAME：当前用户的登录名</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HOSTNAME：指主机的名称</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SHELL：当前用户Shell类型</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;LANGUGE：语言相关的环境变量，多语言可以修改此环境变量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MAIL&#160;&#160;&#160;&#160;当前用户的邮件存放目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;PS1：[\u@\h \W]\$ &#160;&#160;&#160;&#160;基本提示符，对于root用户是#，对于普通用户是$</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;PS2:敲错以后进入的符号</p>
<h4 id="PATH-声明，其格式为"><a href="#PATH-声明，其格式为" class="headerlink" title="PATH 声明，其格式为"></a>PATH 声明，其格式为</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">PATH=<span class="variable">$PATH</span>:&lt;PATH 1&gt;:&lt;PATH 2&gt;:&lt;PATH 3&gt;:------:&lt;PATH N&gt;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/4. Linux 环境变量/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
